package org.example.springboot01.service.impl;


import org.example.springboot01.mapper.Categorymapper;
import org.example.springboot01.pojo.Category;
import org.example.springboot01.service.Categoryservice;
import org.example.springboot01.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class Categoryserviceimpl implements Categoryservice {
    @Autowired
    private Categorymapper categorymapper;
    @Override
    public void addcategory(Category categoryname) {
        //补充信息
        categoryname.setCreatime(LocalDateTime.now().toString());
        categoryname.setUpdatetime(LocalDateTime.now().toString());
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        categoryname.setCreatuser(username);
        categorymapper.addcategory(categoryname);
    }

    @Override
    public List<Category> getcategory() {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        return categorymapper.getcategory(username);

    }

    @Override
    public List<Category> getcategorybyid(Integer id) {
        return categorymapper.getcategorybyid(id);

    }

    @Override
    public void updatecategory(@Validated Category category) {
        category.setUpdatetime(LocalDateTime.now().toString());
        Integer id = category.getId();
        categorymapper.updatecategory(category,id);
    }

    @Override
    public void deletecategory(String name) {
        categorymapper.deletecategory(name);
    }


}
